package com.zhaoyang.dao;

import com.zhaoyang.model.Admin;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 赵扬
 * @time 2020/4/27 10:18
 */
public class AdminDao extends BaseDao {

    /**
     * 根据管理员id修改密码
     *
     * @param id
     * @param newPassword
     * @return
     */
    public boolean editPasswordById(int id, String newPassword) {
        String sql = "update s_admin set password = sha('" + newPassword + "') where id = " + id;
        return update(sql);
    }

    /**
     * 管理员登录
     *
     * @param name
     * @param password
     * @return Admin对象
     */
    public Admin login(String name, String password) {
        String sql = "select * from s_admin where name = '" + name + "' and password = '" + password + "'";
        ResultSet ret = query(sql);
        try {
            if (ret.next()) {
                Admin admin = new Admin();
                admin.setId(ret.getInt("id"));
                admin.setName(ret.getString("name"));
                admin.setPassword(ret.getString("password"));
                admin.setStatus(ret.getInt("status"));
                return admin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
